package ca.tatham.senssim;

import java.util.HashSet;
import java.util.Set;

import ca.tatham.senssim.util.Address;

public abstract class AbstractPacket implements Packet
{
  private static Set<Long> m_sequences = new HashSet<Long>();

  private final Address m_source;
  private final Address m_destination;
  private final long m_sequence;
  private final Address m_sinkAddress;

  protected AbstractPacket(Address source, Address destination,
      Address sinkAddress)
  {
    this(source, destination, sinkAddress, getNextSequence());
  }

  private static long getNextSequence()
  {
    long time = System.nanoTime();
    while (!m_sequences.add(time))
    {
      Simulation.log("Sequence: " + time + " already used");
      time++;
    }
    return time;
  }

  protected AbstractPacket(Address source, Address destination,
      Address sinkAddress, long seq)
  {
    m_source = source;
    m_destination = destination;
    m_sinkAddress = sinkAddress;
    m_sequence = seq;
  }

  public long getSequence()
  {
    return m_sequence;
  }

  public Address getSource()
  {
    return m_source;
  }

  public Address getDestination()
  {
    return m_destination;
  }

  /**
   * @return the original address of the generator of this Interest message
   */
  public Address getSinkAddress()
  {
    return m_sinkAddress;
  }
}
